<?php
	/*
	
		RESIZE ALL IMAGES SCRIPT
		
		1. Remove all resizings
		2. Loop through each image and resize it.
		3. Configurable settings so that you can select how many to do at a time if you have timeout issues
		4. Listing page before processing
		5. Results page after processing with option to do another round
	
	*/

	if($authInfo->usertype != '0') {
		redirectCleanly("/");
	}

	include_once("cmsConfig.php");// this should have already been included..
	include_once(CMS_HOME_DIR . "/lib/imagesModule.php");
	
	$binarydata = new CmsTableWriter(binarydata, $db);
	$cmsDocument = new CmsDocument($db);  
	//$tableWriter = new CmsTableWriter(binarydata,$db);
	//$tableWriter->resetIterator();
	$db2 = new CmsDatabaseConnection(CMS_DSN);
	$query = "Select * from binarydata order by id, filetypecode ";
	$results = $db2->executeQuery($query);
	
	
	$start = extractNumberWithKeyFromArray('start', $_POST, null);
	$end = extractNumberWithKeyFromArray('end', $_POST, null);

	//while (($row = $tableWriter->nextItem())){
			while ($row = $db2->nextObject()) {
				
				//echo "Row = <br><pre>";print_r($row);echo "</pre><br>";
				//echo " filetypecode  = ".$row->filetypecode." <br>";
				
				//check if resizing
				if($row->id >= $start && $row->id <= $end && $row->filetypecode == "1" ) {
					//enter resizing code here
					$record = $cmsDocument->openDocument($row->documentcode);
					//echo "record = <pre>";print_r($record);echo "</pre><br>";
					$query = "Delete from binarydata Where id='".$row->id."' AND elementid='".$row->elementid."' AND filetypecode != '1' ";
					//echo $query;
					$db->executeQuery($query);
					//check the documenttypecode
					$imageDescriptor = new CmsTableWriter(CMS_IMAGE_DESCRIPTOR ,$db);
					$imageDescriptor->resetCustomIterator("Where documenttypecode='".$record->documenttypecode."' And elementid='".$row->elementid."' ");
					while($rowImage = $imageDescriptor->nextItem()){
						//echo "@";
						if($rowImage->documenttypecode == $record->documenttypecode) {
							//Remove other filetypes for this document code / elementid combo
							
							//echo "rowImage = ";print_r($rowImage);echo"</pre><br>";
							resize($binarydata, $db, $row->id, $row->documentcode, $rowImage);
							//echo $message." file id =".$row->id." documentcode =".$row->documentcode." <br>";
						}
						$query = "Update binarydata set documentcode='".$row->documentcode."' Where id='".$row->id."' ";
						$db->executeQuery($query);
					}
				}
			}
			
	$query = "Select * from binarydata order by id, filetypecode ";
	$results = $db2->executeQuery($query);
?>
<html>
	<head>
		<title>Admin Resize Images</title>
	</head>
	<body>
		<h1>Resize Images</h1>
		<h2>Image Listings</h2>
		<table border="1">
			<tr>
				<td>count<? $count = 1?></td><td>id</td><td>documentcode</td><td>elementid</td><td>filetypecode</td><td>filesize</td><td>mimetype</td><td>filename</td>
			</tr>
			<?
				//while (($row = $tableWriter->nextItem())){
				while ($row = $db2->nextObject()) {
				
				
			?>
			<tr>
				<td><?=$count?></td><td><?=$row->id?></td><td><?=$row->documentcode?></td><td><?=$row->elementid?></td><td><?=$row->filetypecode?></td><td><?=$row->filesize?></td><td><?=$row->mimetype?></td><td><?=$row->filename?></td>
			</tr>
			<?
					$count++;
				}
			?>
		</table>
		<br>
		<form action="/cms/html/Admin/cmsResizeAllImages.php" method="POST" enctype="multipart/form-data" id="range">
			Start with Id <input type="text" name="start"><br>
			End with Id <input type="text" name="end"><br>
			<input type="submit" value="Go">
		</form>
	</body>
</html>